package com.cskaoyan.converter;

import lombok.SneakyThrows;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author: lds
 * @clsss: String2DateConConverter
 */
@Component
public class String2DateConConverter implements Converter<String, Date> {
    @SneakyThrows
    @Override
    public Date convert(String s) {

            SimpleDateFormat simpleDateFormat=null;
            if (s.length()==10){
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            }else if (s.length() == 19) {
                simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            }

            Date parse=null;
            try {
                parse = simpleDateFormat.parse(s);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return parse;

    }
}
